package com.cungmua.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.cungmua.app.R;
import com.cungmua.app.handler.CartItemSelectedHandler;
import com.cungmua.app.model.CartItem;
import com.cungmua.app.utils.AppUtils;
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import java.util.List;

/**
 * Created by kelvin.nguyen on 6/25/14.
 */
public class CartItemAdapter extends BaseAdapter {

    private final Context context;
    private final CartItemSelectedHandler handler;
    private List<CartItem> dataSource;
    private final LayoutInflater inflater;

    public CartItemAdapter(Context context, CartItemSelectedHandler handler, List<CartItem> selectedItems) {
        this.context = context;
        this.dataSource = selectedItems;
        this.inflater = LayoutInflater.from(context);
        this.handler = handler;
    }

    public void setDataSource(List<CartItem> dataSource) {
        this.dataSource = dataSource;
        notifyDataSetChanged();
    }

    public List<CartItem> getDataSource() {
        return dataSource;
    }

    @Override
    public int getCount() {
        return dataSource.size();
    }

    @Override
    public CartItem getItem(int i) {
        return dataSource.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    private class ViewHolder {
        public TextView txtSkuName;
        public ImageView imgSku;
        public Spinner spnQuantity;
        public TextView txtMarketPrice;
        public TextView txtPrice;
        private ImageButton btnDelete;
    }
    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        final CartItem item = dataSource.get(pos);
        final ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.sku_items, parent, false);
            holder = new ViewHolder();

            holder.imgSku = (ImageView) convertView.findViewById(R.id.imgSku);
            holder.txtSkuName = (TextView) convertView.findViewById(R.id.txtSkuName);
            holder.spnQuantity = (Spinner) convertView.findViewById(R.id.spnQuantity);
            holder.txtPrice = (TextView) convertView.findViewById(R.id.txtPrice);
            holder.txtMarketPrice = (TextView) convertView.findViewById(R.id.txtMarketPrice);
            holder.btnDelete = (ImageButton) convertView.findViewById(R.id.btnDelete);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.txtPrice.setText(AppUtils.getFullPriceString(item.getPrice()) + "đ ");
        holder.imgSku.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handler.onBackDealDetails(item.getProductId());
            }
        });
        UrlImageViewHelper.setUrlDrawable(holder.imgSku, item.getSmallImage());
        holder.txtSkuName.setText(item.getName());
        final ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item);

        for (int k = item.getMinQuantity(); k <= item.getAvailableQuantity(); k++) {
            spinnerAdapter.add("" + k);
        }
        holder.spnQuantity.setAdapter(spinnerAdapter);
        holder.spnQuantity.setSelection(spinnerAdapter.getPosition("" + item.getQuantity()));

        holder.spnQuantity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                Integer quantity = AppUtils.tryParse(spinnerAdapter.getItem(position));

                holder.txtMarketPrice.setText(AppUtils.getFullPriceString(quantity*item.getPrice()) + "đ ");

                if (quantity != null)
                    item.setQuantity(quantity);
                handler.onCartItemSelected(item);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               handler.deleteSelectedItems(item);
            }
        });
        return convertView;
    }
}

